package com.mali.mq.config;

import com.mali.mq.constant.MqConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Exchange logExchange() {
        return ExchangeBuilder.directExchange(MqConstants.LOG_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue logQueue() {
        return QueueBuilder.durable(MqConstants.LOG_QUEUE).build();
    }

    @Bean
    public Binding logBinding() {
        return BindingBuilder
                .bind(logQueue())
                .to(logExchange())
                .with(MqConstants.LOG_ROUTING_KEY)
                .noargs();
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter(); // 使用JSON序列化
    }
}